﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.DBManager;
using DelonixRegiaHotel.Classes;
using System.Collections;
using System.Globalization;


namespace DelonixRegiaHotel
{
    public partial class UpdateCustomer : Form
    {
        public UpdateCustomer()
        {
            InitializeComponent();
            ArrayList getCustomerInfo = ManageCustomerDBManager.GetAllCustomerDetails();
            cboxnric.Items.Add("-Select Nric-");
            foreach(Customer c in getCustomerInfo)
            {
                cboxnric.Items.Add(c.Nric + ", " + c.CustomerID);
            }
            cboxnric.SelectedIndex = 0;
            List<string> countries = getCountryList();
            cboxcountry.DataSource = countries;
        }

        private void cboxnric_SelectedIndexChanged(object sender, EventArgs e)
        {
            ArrayList getCustomerInfo = ManageCustomerDBManager.GetAllCustomerDetails();
            foreach (Customer c in getCustomerInfo)
            {
                if(cboxnric.SelectedItem.ToString().Contains(c.CustomerID) && cboxnric.SelectedItem.ToString().Contains(c.Nric))
                {
                    lblshowcustomerid.Text = c.CustomerID;
                    tbxfirstname.Text = c.FirstName;
                    tbxlastname.Text = c.LastName;
                    tbxhomeaddress.Text = c.HomeAddress;
                    tbxpostalcode.Text = Convert.ToString(c.PostalCode);
                    for (int i = 0; i <= cboxcountry.Items.Count; i++ )
                    {
                        if(cboxcountry.Items[i].ToString().Equals(c.Country))
                        {
                            cboxcountry.SelectedIndex = i;
                            break;
                        }
                    }
                    tbxdob.Text = Convert.ToString(c.Dob.ToShortDateString());
                    tbxcontactno.Text = Convert.ToString(c.ContactNo);
                    tbxemail.Text = c.Email;
                    tbxnric.Text = c.Nric;
                }
            }
        }

        public List<string> getCountryList()
        {
            List<string> cultureList = new List<string>();
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

            cultureList.Add("-- Select Country --");

            foreach (CultureInfo culture in cultures)
            {
                CultureTypes ct = culture.CultureTypes;
                String s = ct.ToString();
                if (!s.Contains("NeutralCultures"))
                {
                    // check if it's not a invariant culture
                    if (culture.LCID != 127)
                    {
                        RegionInfo region = new RegionInfo(culture.LCID);
                        // add countries that are not in the list
                        if (!(cultureList.Contains(region.EnglishName)))
                        {
                            cultureList.Add(region.EnglishName);
                        }
                    }
                }
            }
            cultureList.Sort(); // sort alphabetically
            return cultureList;
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            DateTime dt;
            if (!DateTime.TryParse(tbxdob.Text, out dt))
            {
                MessageBox.Show("Invalid Date, please check date of birth");
                return;
            }

            if(tbxfirstname.Text.Equals("") || tbxlastname.Text.Equals("") || tbxhomeaddress.Text.Equals("") || tbxpostalcode.Text.Equals("") || cboxcountry.SelectedIndex == 0 || tbxcontactno.Text.Equals("") || tbxemail.Text.Equals("") || tbxnric.Text.Equals(""))
            {
                MessageBox.Show("Please ensure all the information are filled in");
                return;
            }

            Customer updateCustomer = new Customer();
            updateCustomer.CustomerID = lblshowcustomerid.Text;
            updateCustomer.FirstName = tbxfirstname.Text;
            updateCustomer.LastName = tbxlastname.Text;
            updateCustomer.HomeAddress = tbxhomeaddress.Text;
            updateCustomer.PostalCode = Convert.ToInt32(tbxpostalcode.Text);
            updateCustomer.Country = cboxcountry.SelectedItem.ToString();
            updateCustomer.Dob = Convert.ToDateTime(tbxdob.Text);
            updateCustomer.ContactNo = Convert.ToInt32(tbxcontactno.Text);
            updateCustomer.Email = tbxemail.Text;
            updateCustomer.Nric = tbxnric.Text;
            if (ManageCustomerDBManager.UpdateCustomer(updateCustomer) == 1)
            {
                MessageBox.Show("Updated " + updateCustomer.CustomerID + " Sucessfully");
                this.Hide();
            }
        }

        private void tbxCheckAllForNumbers_TextChanged(object sender, EventArgs e) // int only generalized method
        {
            Exception X = new Exception();
            TextBox T = (TextBox)sender;

            try
            {
                if (T.Text != "-")
                {
                    int x = int.Parse(T.Text);
                }
            }
            catch (Exception)
            {
                try
                {
                    int CursorIndex = T.SelectionStart - 1;
                    T.Text = T.Text.Remove(CursorIndex, 1);

                    //Align Cursor to same index
                    T.SelectionStart = CursorIndex;
                    T.SelectionLength = 0;
                }
                catch (Exception) { }
            }
        }


    }
}

